package com.lianniu.web.sellerOrder.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.product.dao.ISkuProductDao;
import com.lianniu.web.sellerOrder.dao.ISellerOrderDao;
import com.lianniu.web.sellerOrder.service.ISellerOrderService;
import com.lianniu.web.sellerOrder.vo.CancelOrder;
import com.lianniu.web.sellerOrder.vo.OrderInfoVo;
import com.lianniu.web.sellerOrder.vo.SellerOrderConditionVo;
import com.lianniu.web.sellerOrder.vo.ShipmentVo;
import com.lianniu.web.util.StringUtil;
@Service
public class SellerOrderServiceImpl implements ISellerOrderService{

  private static final Logger logger = LoggerFactory.getLogger(SellerOrderServiceImpl.class);
  @Autowired
  ISellerOrderDao sellerOrderDao;
  @Autowired
  ISkuProductDao skuProductDao;
  @Override
  public BaseResult getOrderInfo(String orderId) {
    if(StringUtil.isEmpty(orderId)){
      logger.info("orderid 为空，返回错误");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info(String.format("获取订单%s订单详情", orderId));
    OrderInfoVo order = sellerOrderDao.getOrderById(orderId);
    logger.info("获取到的订单详情为："+JSON.toJSONString(order));
    return BaseResult.success(order);
  }
  @Override
  public BaseResult getOrders(SellerOrderConditionVo condition) {
    logger.info("获取卖家订单列表,condition="+JSON.toJSONString(condition));
    if(StringUtils.isBlank(condition.getSellerUserId())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    List<OrderInfoVo> orders = sellerOrderDao.getOrders(condition);
    logger.info("获取到的订单列表为："+JSON.toJSONString(orders));
    return BaseResult.success(orders);
  }
  @Override
  public BaseResult getTrend(int pageNum) {
    logger.info("获取卖家详情");
    int start = 10 * pageNum;
    List<OrderInfoVo> orders = sellerOrderDao.getTrend(start,pageNum);
    logger.info("获取到的详情订单列表为："+JSON.toJSONString(orders));
    return BaseResult.success(orders);
  }
  @Override
  @Transactional
  public BaseResult cancelOrder(CancelOrder cancel) {
    
    logger.info("取消订单："+JSON.toJSONString(cancel));
    if(StringUtils.isBlank(cancel.getOrderid())
        || StringUtils.isBlank(cancel.getUserid())
        || StringUtils.isBlank(cancel.getCancelReason())){
      logger.info("有参数为空，直接返回失败");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(cancel.getCancelReason().length() > CancelOrder.CANCEL_REASON_MAX_LENGTH){
      logger.info("取消原因长度过长："+cancel.getCancelReason().length());
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    skuProductDao.addOverPlusNumBySkuId(cancel.getOverPlusNum(), cancel.getSkuId());
    sellerOrderDao.cancelOrder(cancel);
    sellerOrderDao.cancelOrderDetail(cancel);
    return BaseResult.success();
  }
  @Override
  public BaseResult shipment(ShipmentVo ship) {
    sellerOrderDao.shipment(ship);
    return BaseResult.success();
  }

}
